/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.ppm.dat.view;

import bgu.ppm.dat.model.Table;
import bgu.sonar.util.evt.EventListeners;
import java.util.logging.Logger;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author User
 */
public class PivotJTableModel implements TableModel {

    private Table innerModel;
    private int numberOfHeaderRows = -1;
    private int numberOfHeaderColumns = -1;
    private EventListeners<TableModelListener> listeners = EventListeners.create(TableModelListener.class);
    private static final Logger LOG = Logger.getLogger(PivotJTableModel.class.getName());

    public PivotJTableModel(Table innerModel) {
        this.innerModel = innerModel;
    }

    @Override
    public int getRowCount() {
        final int rowCount = innerModel.getRowHeaders().length + getNumberOfColumnHeaders();
//        LOG.log(Level.INFO, "Row Count: {0}", rowCount);
        return rowCount;
    }

    public Table getInnerModel() {
        return innerModel;
    }
    
    @Override
    public int getColumnCount() {
        final int colCount = innerModel.getColumnHeaders().length + getNumberOfRowHeaders();
//        LOG.log(Level.INFO, "Col Count: {0}", colCount);
        return colCount;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return "";
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return Object.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex < getNumberOfColumnHeaders()) {
            if (columnIndex < getNumberOfRowHeaders()) {
                return "";
            }

            return innerModel.getColumnHeaders()[columnIndex - getNumberOfRowHeaders()][rowIndex].toString();
        }

        if (columnIndex < getNumberOfRowHeaders()) {
            return innerModel.getRowHeaders()[rowIndex - getNumberOfColumnHeaders()][columnIndex].toString();
        }



        return innerModel.getCell(columnIndex - getNumberOfRowHeaders(), rowIndex - getNumberOfColumnHeaders());

    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not Supported Yet!");

    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        listeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        listeners.remove(l);
    }

    private int getNumberOfRowHeaders() {
        if (numberOfHeaderRows == -1) {
            if (innerModel.getRowHeaders().length > 0) {
                numberOfHeaderRows = innerModel.getRowHeaders()[0].length;
            } else {
                numberOfHeaderRows = 0;
            }
        }


        return numberOfHeaderRows;
    }

    private int getNumberOfColumnHeaders() {
        if (numberOfHeaderColumns == -1) {
            if (innerModel.getColumnHeaders().length > 0) {
                numberOfHeaderColumns = innerModel.getColumnHeaders()[0].length;
            } else {
                numberOfHeaderColumns = 0;
            }
        }


        return numberOfHeaderColumns;
    }
}
